package com.mymvc.exception;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 王俊彪
 * @version 1.0
 */
@Component
public class MyExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response,
                                         Object handler, Exception ex) {

        System.out.println(handler);
        //将错误信息记录到日志文件
        Logger logger = LogManager.getLogger("fileLogger");
        logger.error(handler+":"+ex.getMessage()+'，'+ex);

        //转发一个页面
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        modelAndView.addObject("message","请联系管理员");
        return modelAndView;


    }
}
